﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeC.Models;
using System.ComponentModel.Composition;
using CodeC.Interfaces;
using System.Text.RegularExpressions;

namespace CodeC.Utilities
{

    /// <summary>
    /// This class helps to handle the File operations and Code compile engine
    /// </summary>
    [Export]
    public class CodeCHelper
    {
        #region Private Variable

        private string _codeCExtension = ".codec";
        private string _codeCSeparator = "[#CODEC#]";
        private FileOperations _fileOperations;
        private XMLFileOperations _xmlFileOperations;

        [Import]
        public ICodeCEngine _codeCEngine = null;

        [Import]
        public ICreateClassCode _createClassCode = null;

        #endregion

        #region ctor

        [ImportingConstructor]
        public CodeCHelper(FileOperations fileOperations, XMLFileOperations xmlFileOperations)
        {
            _fileOperations = fileOperations;
            _xmlFileOperations = xmlFileOperations;
        }

        #endregion

        #region Public Methods

        public IOutputModel<string> Run(ICodeCModel model)
        {
            return _codeCEngine.CompileCode(_createClassCode.GenerateClassString(model), model.ListOfReferences);
        }

        public ICodeCFileModel SaveCode(ICodeCModel model, ICodeCFileModel fileModel)
        {
            string filename = string.Empty;
            if (fileModel.File.IsNullOrEmpty())
                filename = fileModel.Name.RemoveSpecialChar().Replace(" ", "") + _codeCExtension;
            else
                filename = fileModel.File;

            string content = string.Format("{0}{3}{1}{3}{2}", fileModel.Name, model.MainCode, model.ClassMethodsCode, _codeCSeparator);
            if (fileModel.ID > 0)
                _fileOperations.Delete(filename);

            string outFileName = _fileOperations.Write(filename, content);
            fileModel.File = outFileName;
            fileModel = (_xmlFileOperations).Write(fileModel);

            return fileModel;
        }

        public ICodeCModel GetCodeModel(ICodeCFileModel fileModel)
        {
            string filename = string.Empty;

            if (fileModel.File.IsNullOrEmpty())
                filename = fileModel.Name.RemoveSpecialChar().Replace(" ", "") + _codeCExtension;
            else
                filename = fileModel.File;

            string content = _fileOperations.Read(filename);
            var contentArrary = content.Split(new string[] { "[#CODEC#]" }, StringSplitOptions.None);

            return new CodeCModel
                        {
                            MainCode = contentArrary[1],
                            ClassMethodsCode = contentArrary[2]
                        };
        }

        public List<ICodeCFileModel> GetListOfFileModel()
        {
            return (_xmlFileOperations).Read();
        }

        public bool RemoveCode(ICodeCFileModel fileModel)
        {
            string filename = fileModel.File;

            (_xmlFileOperations).Delete(fileModel);

            _fileOperations.Delete(filename);



            return true;
        }

        #endregion
    }
}
